package com.ck.company.service.impl;

import com.ck.common.service.OperationLogService;
import com.ck.company.service.CpSceneService;
import com.ck.constant.ModuleConstant;
import com.ck.dao.SceneBottomMapper;
import com.ck.dao.SceneMapper;
import com.ck.dao.SceneMiddleMapper;
import com.ck.dao.SceneTopMapper;
import com.ck.exception.ServiceException;
import com.ck.model.common.Scene;
import com.ck.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 场景
 *
 * @author linqiang
 * @date 2018-06-20 18:33
 */
@Service(value = "cpSceneService")
public class CpSceneServiceImpl implements CpSceneService {

    @Autowired
    private SceneMapper sceneMapper;
    @Autowired
    private SceneTopMapper sceneTopMapper;
    @Autowired
    private SceneMiddleMapper sceneMiddleMapper;
    @Autowired
    private SceneBottomMapper sceneBottomMapper;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 添加场景
     *
     * @param sceneName 场景名称
     * @param request
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveScene(String sceneName, HttpServletRequest request) {
        int row = 0;
        Scene scene = new Scene();
        scene.setAddTime(System.currentTimeMillis());
        scene.setProgramVersion(UUIDUtils.getUUIDString());
        scene.setSceneName(sceneName);
        row = sceneMapper.insert(scene);
        operationLogService.saveSuccessOperationLog("保存场景成功，场景版本号为："
                + scene.getSceneVersion(), ModuleConstant.MODULE_PROGRAM, request);
        return row;
    }

    /**
     * 删除场景
     *
     * @param sceneId 场景主键
     * @param request
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteScene(Long sceneId, HttpServletRequest request) {
        int row = 0;
        Scene scene = sceneMapper.selectByPrimaryKey(sceneId);
        if (null != scene) {
            //删除场景的上中下块
            sceneTopMapper.deleteBySceneVersion(scene.getSceneVersion());
            sceneMiddleMapper.deleteBySceneVersion(scene.getSceneVersion());
            sceneBottomMapper.deleteBySceneVersion(scene.getSceneVersion());
            row = sceneMapper.deleteByPrimaryKey(sceneId);
            operationLogService.saveSuccessOperationLog("删除场景成功，场景版本号为："
                    + scene.getSceneVersion(), ModuleConstant.MODULE_PROGRAM, request);
        }
        return row;
    }

    /**
     * 根据公司编码查询场景版本号集合
     *
     * @param companyCode 商家编码
     * @return List<String>
     */
    @Override
    public List<String> listSceneVersionByCompanyCode(String companyCode) {
        return sceneMapper.listSceneVersionByCompanyCode(companyCode);
    }

    /**
     * 通过URL获取不能删除节目的条数（节目状态为1：待审批，2：待发布）
     * @param url 地址
     * @return Long
     * @throws ServiceException
     */
    @Override
    public Long countNoDeleteProgram(String url) throws ServiceException {
        if (org.apache.commons.lang3.StringUtils.isBlank(url)) {
            throw new ServiceException("地址不能为空");
        }
        return sceneMapper.countNoDeleteProgram(url);
    }
}
